/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sadhar.sipp.perekamankodeglunitkerja;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.sadhar.errhandler.ClassAntiNull;
import org.sadhar.sia.common.ClassConnection;
import org.springframework.jdbc.core.RowMapper;

/**
 *
 * @author hendro
 */
public class PerekamanKodeGLUnitKerjaDAOImpl implements PerekamanKodeGLUnitKerjaDAO{

    public PerekamanKodeGLUnitKerjaDAOImpl(){
        ClassConnection.getTransactionProxyFactoryBean().setTarget(this);
    }
    public List<PerekamanKodeGLUnitKerja> list() throws Exception {
       List<PerekamanKodeGLUnitKerja> list = new ArrayList<PerekamanKodeGLUnitKerja>();
       List<Map> rows = ClassConnection.getJdbc().queryForList("SELECT * FROM kamus.unkerja ORDER BY idIndukUnitKerja ASC");
       for(Map m : rows){
           PerekamanKodeGLUnitKerja unit = new PerekamanKodeGLUnitKerja();
           unit.setKd_unit_kerja(ClassAntiNull.AntiNullString(m.get("Kd_unit_kerja")));
           unit.setNama_unit_kerja(ClassAntiNull.AntiNullString(m.get("Nama_unit_kerja")));
           unit.setIdIndukUnitKerja(ClassAntiNull.AntiNullInt(m.get("idIndukUnitKerja")));
           unit.setIdUnitKerja(ClassAntiNull.AntiNullInt(m.get("idUnitKerja")));
           unit.setKodeGLUnit(ClassAntiNull.AntiNullString(m.get("kodeGLUnit")));
           list.add(unit);
       }
       return list;
    }

    public void update(PerekamanKodeGLUnitKerja unitKerja) throws Exception {
        String sql = "UPDATE kamus.unkerja SET kodeGLUnit=? WHERE Kd_unit_kerja=?";
        ClassConnection.getJdbc().update(sql,new Object[]{unitKerja.getKodeGLUnit(),unitKerja.getKd_unit_kerja()});
    }

    public PerekamanKodeGLUnitKerja select(int idUnitKerja) throws Exception {
        String sql = "SELECT * FROM kamus.unkerja WHERE idUnitKerja=?";
        PerekamanKodeGLUnitKerja unit = new PerekamanKodeGLUnitKerja();
        unit = (PerekamanKodeGLUnitKerja)ClassConnection.getJdbc().queryForObject(sql, new Object[]{idUnitKerja},new RowMapper(){

            public Object mapRow(ResultSet rs, int numRow) throws SQLException {
                return new PerekamanKodeGLUnitKerja(
                        ClassAntiNull.AntiNullInt(rs.getInt("idUnitKerja")),
                        ClassAntiNull.AntiNullInt(rs.getInt("idIndukUnitKerja")),
                        ClassAntiNull.AntiNullString(rs.getString("Kd_unit_kerja")),
                        ClassAntiNull.AntiNullString(rs.getString("Nama_unit_kerja")),
                        ClassAntiNull.AntiNullString(rs.getString("kodeGLUnit")));
            }

        });
        return unit;
    }

}
